package com.zmx.patients.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patients.R;
import com.zmx.patients.adapter.ArticleMessageAdapter;
import com.zmx.patients.adapter.CategoriesAdapter;
import com.zmx.patients.fragment.friends.FocusOnFragment;
import com.zmx.patients.fragment.friends.RecommendedFragment;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.ArticleMessage;
import com.zmx.patients.pojo.Categories;
import com.zmx.patients.view.activity.ArticleDetailsActivity;
import com.zmx.patients.view.activity.CircleOfFriendsActivity;
import com.zmx.patients.view.activity.ReleaseFriendsActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InformationFragment extends Fragment {

//    private CategoriesAdapter c_adaper;
//    private List<Categories> dps;
//    private RecyclerView recyclerView;
//
//
//    private ArticleMessageAdapter adapter;
//    private List<ArticleMessage> lists;
//    private RecyclerView re_doctor;
//    private RefreshLayout mRefreshLayout;
//
//    private String column_id;

    TabLayout tab_layout;
    ViewPager viewPager;
    static final int NUM_ITEMS = 2;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private String[] strings = new String[]{"推荐","最新"};

    private ImageButton add_button;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_information, container, false);

//        dps = new ArrayList<>();
//        recyclerView = view.findViewById(R.id.recycler_view);
//        recyclerView.setLayoutManager(new GridLayoutManager(InformationFragment.this.getActivity(), 4));
//
//
//        adapter = new ArticleMessageAdapter(InformationFragment.this.getActivity());
//        re_doctor = view.findViewById(R.id.re_doctor);
//        re_doctor.setLayoutManager(new LinearLayoutManager(InformationFragment.this.getActivity()));
//        re_doctor.setAdapter(adapter);
//        lists = new ArrayList<>();
//        adapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            protected void onItemClick(View view, int listPosition) {
//
//                //目前Activity→目标Activity
//                Intent intent = new Intent(InformationFragment.this.getActivity(), ArticleDetailsActivity.class);
//                intent.putExtra("am",lists.get(listPosition));
//                startActivity(intent);
//
//            }
//        });
//        // 开启自动加载功能
//        mRefreshLayout = view.findViewById(R.id.refreshLayout);
//        mRefreshLayout.setEnableAutoLoadMore(true);
//        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
//
//            getArticle();
//
//        });
//
//        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
//
//            mRefreshLayout.finishLoadMore(true);
//
//        });
//
//        getColumn();
        tab_layout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.viewPager);
        add_button = view.findViewById(R.id.add_button);
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(InformationFragment.this.getContext(), ReleaseFriendsActivity.class);
                startActivity(intent);

            }
        });

        RecommendedFragment lif = new RecommendedFragment();
        FocusOnFragment ltf = new FocusOnFragment();

        fragmentList.add(lif);
        fragmentList.add(ltf);

        initView();
        return view;
    }


    private void initView(){


        MyAdapter fragmentAdater = new MyAdapter(getFragmentManager());
        viewPager.setAdapter(fragmentAdater);
        tab_layout.setupWithViewPager(viewPager);

    }


    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return strings[position];
        }
    }


//
//    @SuppressLint("HandlerLeak")
//    private Handler handler = new Handler(){
//
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//
//            switch (msg.what){
//
//                case 1:
//
//                    try {
//                        Log.e("fs发顺丰","法大师傅"+msg.obj.toString());
//                        lists.clear();
//                        JSONObject object = new JSONObject(msg.obj.toString());
//                        JSONArray array = object.getJSONArray("data");
//
//                        Gson gson = new Gson();
//                        for (int i = 0; i<array.length();i++){
//
//                            JSONObject jsonObject = array.getJSONObject(i);
//                            ArticleMessage articleMessage = gson.fromJson(jsonObject.toString(),ArticleMessage.class);
//                            lists.add(articleMessage);
//
//                        }
//                        adapter.setListAndNotifyDataSetChanged(lists);
//                        //刷新加载
//                        mRefreshLayout.finishRefresh(true);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//
//                    break;
//
//                case 2:
//
//
//                    try {
//
//                        JSONObject object = new JSONObject(msg.obj.toString());
//                        JSONArray array = object.getJSONArray("data");
//
//                        Gson gson = new Gson();
//                        for (int i = 0; i<array.length();i++){
//
//                            JSONObject jsonObject = array.getJSONObject(i);
//                            Categories c = gson.fromJson(jsonObject.toString(),Categories.class);
//
//                            if (i == 0){
//
//                                column_id = c.getId()+"";
//                            }
//
//                            dps.add(c);
//
//                        }
//
//                        c_adaper = new CategoriesAdapter(dps,InformationFragment.this.getActivity());
//                        recyclerView.setAdapter(c_adaper);
//                        c_adaper.setGetListener(position -> {
//
////                         把点击的下标回传给适配器 确定下标
//                            column_id = dps.get(position).getId()+"";
//                            c_adaper.setmPosition(position);
//                            c_adaper.notifyDataSetChanged();
//                            //触发自动刷新
//                            mRefreshLayout.autoRefresh();
//
//                        });
//                        //触发自动刷新
//                        mRefreshLayout.autoRefresh();
//
//                    } catch (JSONException e) {
//                        Log.e("fs发顺丰","法大师傅"+e.toString());
//                        e.printStackTrace();
//                    }
//
//
//                    break;
//            }
//
//        }
//    };
//
//
//    public void getArticle() {
//
//        Log.e("column_id","column_idssssss"+column_id);
//
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("data[row]",1+"");
//        params.put("data[number]",10+"");
//        params.put("data[column_id]",column_id);
//        params.put("cmd","getArticle");
//        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/article/", params, handler, 1, 404);
//
//    }
//
//    public void getColumn() {
//
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("data[client]",1+"");
//        params.put("data[type]","top");
//        params.put("cmd","getColumn");
//        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/article/", params, handler, 2, 404);
//
//    }

}


